package View.ChartFrame;

import com.mindfusion.charting.GridType;
import com.mindfusion.charting.LabelKinds;
import com.mindfusion.charting.Series2D;
import com.mindfusion.charting.swing.LineChart;
import com.mindfusion.drawing.Brushes;
import com.mindfusion.drawing.DashStyle;
import com.mindfusion.drawing.FontStyle;
import com.mindfusion.drawing.SolidBrush;

import javax.swing.*;
import java.awt.*;
import java.util.Arrays;
import java.util.List;

public class MainWindow extends JFrame {
    private static final long serialVersionUID = 1L;

    //the main method of the application//the main method of the application
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                try {
                    new MainWindow().setVisible(true);
                }
                catch (Exception exp) {
                }
            }
        });
    }

    public MainWindow() {
//        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(650, 650);
        setTitle("Chart Frame");
        getContentPane().add(initializeChart(), BorderLayout.CENTER);
    }

    public MainWindow(String title, List xData, List yData, List<String> labels) {
        setSize(650, 650);
        setTitle(title);
        if ("最短距离".equals(title)) getContentPane().add(initializeChart(xData, yData, labels), BorderLayout.CENTER);
        else getContentPane().add(getEvChart(xData, yData, labels), BorderLayout.CENTER);

    }

    public  MainWindow(String title, List xData, List yData) {
        setSize(650, 650);
        setTitle(title);
        System.out.println("新建了" + title + yData.size());
        if ("包围情况Surround".equals(title)) getContentPane().add(initializeSurroundChart(xData, yData), BorderLayout.CENTER);

    }

    public  MainWindow(String title, List xData, List... yData) {
        setSize(650, 650);
        setTitle(title);
        if ("船队位置情况X".equals(title)){
            for (List item : yData) {
                getContentPane().add(initializeSurroundChart(xData, item), BorderLayout.CENTER);
            }
        }

    }


    private LineChart initializeShipsXChart(
            List xData,
            List yData
    ) {
        LineChart lineChart = new LineChart();

        lineChart.getXAxis().setMinValue(0.0);
        lineChart.getXAxis().setMaxValue(25.0);
        lineChart.getXAxis().setInterval(1.0);
        lineChart.getXAxis().setOrigin(0.0);
        lineChart.getXAxis().setTitle("X坐标/米");
        // Y坐标轴
        lineChart.getYAxis().setMinValue((double) 0);
        lineChart.getYAxis().setMaxValue(1.0);
        lineChart.getYAxis().setInterval(0.1);
        lineChart.getYAxis().setOrigin(0.0);
        lineChart.getYAxis().setTitle("时间/秒");

        //styling the grid
        lineChart.setGridType(GridType.Crossed);
        lineChart.getTheme().setGridLineColor(new Color(192, 192, 192));
        lineChart.getTheme().setGridLineStyle(DashStyle.Dot);

        lineChart.getLegendRenderer().setShowTitle(false);
        lineChart.getTheme().setDataLabelsFontSize(5);

        lineChart.getTheme().setHighlightStroke(Brushes.Orange);

        Series2D series1 = new Series2D(
                xData,
                yData,
                null);

        series1.setSupportedLabels(LabelKinds.XAxisLabel);
        lineChart.getSeries().add(series1);
        Arrays.asList(

                new SolidBrush( new Color (102, 154, 204, 190)),

                new SolidBrush(new Color (156, 170, 198, 190)));

        lineChart.getTheme().setCommonSeriesStrokes(

                Arrays.asList(

                        new SolidBrush( new Color (28, 58, 88))));

        lineChart.getTheme().setCommonSeriesStrokeThicknesses(

                Arrays.asList(5.0));

        return lineChart;
    }




    private LineChart initializeSurroundChart(
            List xData,
            List yData
    ) {
        LineChart lineChart = new LineChart();

        lineChart.getXAxis().setMinValue(0.0);
        lineChart.getXAxis().setMaxValue(25.0);
        lineChart.getXAxis().setInterval(1.0);
        lineChart.getXAxis().setOrigin(0.0);
        lineChart.getXAxis().setTitle("Time (/Per Second)");
        // Y坐标轴
        lineChart.getYAxis().setMinValue((double) 0);
        lineChart.getYAxis().setMaxValue(1.0);
        lineChart.getYAxis().setInterval(0.1);
        lineChart.getYAxis().setOrigin(0.0);
        lineChart.getYAxis().setTitle("Surround(t)");


        lineChart.getTheme().setAxisTitleFontSize(20);
        lineChart.getTheme().setAxisTitleFontStyle(FontStyle.fromInt(2));
//        lineChart.getTheme().setAxisLabelsFontSize(30);

        //styling the grid
        lineChart.setGridType(GridType.Crossed);
        lineChart.getTheme().setGridLineColor(new Color(192, 192, 192));
        lineChart.getTheme().setGridLineStyle(DashStyle.Dot);

        lineChart.getLegendRenderer().setShowTitle(false);
        lineChart.getTheme().setDataLabelsFontSize(30);
        lineChart.getTheme().setAxisLabelsFontSize(15);
        lineChart.getTheme().setAxisTitleFontStyle(FontStyle.fromInt(4));

        lineChart.getTheme().setHighlightStroke(Brushes.Black);

        Series2D series1 = new Series2D(
                xData,
                yData,
                null);

        series1.setSupportedLabels(LabelKinds.XAxisLabel);
        lineChart.getSeries().add(series1);
        Arrays.asList(

                new SolidBrush( new Color (102, 154, 204, 190)),

                new SolidBrush(new Color (156, 170, 198, 190)));

        lineChart.getTheme().setCommonSeriesStrokes(

                Arrays.asList(

                        new SolidBrush( new Color (28, 58, 88))));

        lineChart.getTheme().setCommonSeriesStrokeThicknesses(

                Arrays.asList(2.0));

        return lineChart;
    }






    private LineChart getEvChart(List xData, List yData, List<String> labels) {
        LineChart lineChart = new LineChart();

        lineChart.getXAxis().setMinValue(0.0);
        lineChart.getXAxis().setMaxValue(35.0);
        lineChart.getXAxis().setInterval(1.0);
        lineChart.getXAxis().setOrigin(0.0);
        lineChart.getXAxis().setTitle("Time (/Per Second)");
        // Y坐标轴
        lineChart.getYAxis().setMinValue((double) 0);
        lineChart.getYAxis().setMaxValue(150.0);
        lineChart.getYAxis().setInterval(25.0);
        lineChart.getYAxis().setOrigin(0.0);
        lineChart.getYAxis().setTitle("Mean Distance (/Meter)");

        //styling the grid
        lineChart.setGridType(GridType.Crossed);
        lineChart.getTheme().setGridLineColor(new Color(192, 192, 192));
        lineChart.getTheme().setGridLineStyle(DashStyle.Dot);

        lineChart.getLegendRenderer().setShowTitle(false);
        lineChart.getTheme().setDataLabelsFontSize(12);
        lineChart.getTheme().setAxisLabelsFontSize(15);
        lineChart.getTheme().setHighlightStroke(Brushes.Orange);

        Series2D series1 = new Series2D(
                xData,
                yData,
                labels);

        series1.setSupportedLabels(LabelKinds.XAxisLabel);
        lineChart.getSeries().add(series1);
        Arrays.asList(

                new SolidBrush( new Color (102, 154, 204, 190)),

                new SolidBrush(new Color (156, 170, 198, 190)));

        lineChart.getTheme().setCommonSeriesStrokes(

                Arrays.asList(

                        new SolidBrush( new Color (28, 58, 88))));

        lineChart.getTheme().setCommonSeriesStrokeThicknesses(

                Arrays.asList(2.0));

        return lineChart;
    }










    private LineChart initializeChart(List xData, List yData, List<String> labels) {
        LineChart lineChart = new LineChart();

        lineChart.getXAxis().setMinValue(0.0);
        lineChart.getXAxis().setMaxValue(35.0);
        lineChart.getXAxis().setInterval(1.0);
        lineChart.getXAxis().setOrigin(0.0);
        lineChart.getXAxis().setTitle("time (/Per Second)");
        // Y坐标轴
        lineChart.getYAxis().setMinValue((double) 0);
        lineChart.getYAxis().setMaxValue(5.0);
        lineChart.getYAxis().setInterval(0.5);
        lineChart.getYAxis().setOrigin(0.0);
        lineChart.getYAxis().setTitle("min distance (/Meter)");

        //styling the grid
        lineChart.setGridType(GridType.Crossed);
        lineChart.getTheme().setGridLineColor(new Color(192, 192, 192));
        lineChart.getTheme().setGridLineStyle(DashStyle.Dot);

        lineChart.getLegendRenderer().setShowTitle(false);
        lineChart.getTheme().setDataLabelsFontSize(12);
        lineChart.getTheme().setAxisLabelsFontSize(15);
        lineChart.getTheme().setHighlightStroke(Brushes.Orange);

        Series2D series1 = new Series2D(
                xData,
                yData,
                labels);

        series1.setSupportedLabels(LabelKinds.XAxisLabel);
        lineChart.getSeries().add(series1);
        Arrays.asList(

                new SolidBrush( new Color (102, 154, 204, 190)),

                new SolidBrush(new Color (156, 170, 198, 190)));

        lineChart.getTheme().setCommonSeriesStrokes(

                Arrays.asList(

                        new SolidBrush( new Color (28, 58, 88))));

        lineChart.getTheme().setCommonSeriesStrokeThicknesses(

                Arrays.asList(2.0));

        return lineChart;
    }


    private LineChart initializeChart() {
        LineChart lineChart = new LineChart();

//        lineChart.setXAxis();

        lineChart.getXAxis().setMinValue(-5.0);
        lineChart.getXAxis().setMaxValue(5.0);
        lineChart.getXAxis().setInterval(0.5);
        lineChart.getXAxis().setOrigin(0.0);
        lineChart.getXAxis().setTitle("X");

        lineChart.getYAxis().setMaxValue(30.0);
        lineChart.getYAxis().setMinValue(-30.0);
        lineChart.getYAxis().setTitle("Y");

        //styling the grid
        lineChart.setGridType(GridType.Crossed);
        lineChart.getTheme().setGridLineColor(new Color(192, 192, 192));
        lineChart.getTheme().setGridLineStyle(DashStyle.Dot);

        lineChart.getLegendRenderer().setShowTitle(false);
        lineChart.getTheme().setDataLabelsFontSize(12);
        lineChart.getTheme().setAxisLabelsFontSize(15);
        lineChart.getTheme().setHighlightStroke(Brushes.Orange);

        List<String> labels = Arrays.asList(
                "Jan", "Feb"
        );

        Series2D series1 = new Series2D(

                Arrays.asList(0.0, 1.0),

                Arrays.asList(25.0, 35.0),

                null);


        series1.setSupportedLabels(LabelKinds.XAxisLabel);
        lineChart.getSeries().add(series1);
        Arrays.asList(

                new SolidBrush( new Color (102, 154, 204, 190)),

                new SolidBrush(new Color (156, 170, 198, 190)));

        lineChart.getTheme().setCommonSeriesStrokes(

                Arrays.asList(

                        new SolidBrush( new Color (28, 58, 88))));

        lineChart.getTheme().setCommonSeriesStrokeThicknesses(

                Arrays.asList(2.0));

        return lineChart;
    }

}
